package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-12
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    EduVideoService eduVideoService;
    @Autowired
    public void setEduVideoService(EduVideoService eduVideoService) {
        this.eduVideoService = eduVideoService;
    }
    VodClient vodClient;
    @Autowired
    public void setVodClient(VodClient vodClient) {
        this.vodClient = vodClient;
    }

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 查找所有章节跟小节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(wrapperVideo);

        // 封装数据
        List<ChapterVo> chapterVoList = new ArrayList<>();
        for(int i =0; i < chapterList.size(); i++){
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            // 属性传递
            BeanUtils.copyProperties(eduChapter,chapterVo);
            for(EduVideo item:videoList){
                if(item.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    // 属性传递
                    BeanUtils.copyProperties(item,videoVo);
                    chapterVo.getChildren().add(videoVo);
                }
            }
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    // 删除章节方法
    @Override
    public boolean deleteChapter(String chapterId) {
        // 查询该章节是否有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);
        // 有小节就删除
        if(eduVideoList.size()>0) {
            List<String> videoSourceIdList = new ArrayList<>();
            for (EduVideo item : eduVideoList) {
                if (!StringUtils.isEmpty(item.getVideoSourceId())) {
                    videoSourceIdList.add(item.getVideoSourceId());
                }
            }
            vodClient.deleteVideoList(videoSourceIdList);
        }
        // 删除章节信息
        int result = baseMapper.deleteById(chapterId);
        return result > 0;

    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
